package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public int add(UserBean user) {
        //得到会话
        SqlSession session=this.getSession();
        //得到指定mapper接口的实现类对象
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        //完成添加
        mapper.add(user);
        //提交事务
        session.commit();

        //关闭
        session.close();
        return user.getId();


    }

    @Override
    public void del(int id) {
        //得到会话
        SqlSession session=this.getSession();
        //得到指定mapper接口的实现类对象
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        //完成添加
        mapper.del(id);;
        //提交事务
        session.commit();

        //关闭
        session.close();

    }

    @Override
    public void update(@Param("userId") int id,
                       @Param("pwd") String pwd) {
        //得到会话
        SqlSession session=this.getSession();
        //得到指定mapper接口的实现类对象
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        //完成添加
        mapper.update(id,pwd);;
        //提交事务
        session.commit();

        //关闭
        session.close();

    }

    @Override
    public List<UserBean> findAll() {
        //得到会话
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        List<UserBean>list=mapper.findAll();

        //关闭
        session.close();

        return list;
    }

    @Override
    public List<UserBean> findByName(String name) {
        SqlSession session= this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        List<UserBean>list=mapper.findByName(name);
        session.close();
        return list;
    }

    @Override
    public List<UserBean> findByBirthday(LocalDate startDate, LocalDate endDate) {
        SqlSession session= this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        List<UserBean>list=mapper.findByBirthday(startDate,endDate);
        session.close();
        return list;
    }

    @Override
    public void addMore(List<UserBean> list) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        mapper.addMore(list);



        session.commit();
        session.close();

    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        mapper.delMore(idArray);



        session.commit();
        session.close();

    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session=this.getSession();
        IUserMapper mapper=session.getMapper(IUserMapper.class);
        List<UserBean>list=mapper.findByItem(name,startDate,endDate);
        session.commit();
        session.close();


        return list;
    }

    @Override
    public CutPageBean<UserBean> cutAll(int pageNO) {
        CutPageBean<UserBean>cunBean=new CutPageBean<>();
        SqlSession session=this.getSession();
        IUserMapper mapper= session.getMapper(IUserMapper.class);
        //得到当前页需要显示的数据

        List<UserBean>list=mapper.cutAllList((pageNO-1)*PAGESIZE,PAGESIZE);
        //得到总记录数
        int count=mapper.cutAllCount();
        //得到总页数
        int totalPage=count%PAGESIZE==0?
                count/PAGESIZE:count/PAGESIZE+1;
        //将数据封装为分页对象
        cunBean.setList(list);
        cunBean.setCount(count);
        cunBean.setTitlePage(totalPage);
        session.close();
        return cunBean;
    }

    public static void main(String[] args) {
        IUserService service=new UserServiceImpl();
//        service.add(new UserBean("周强","000", LocalDate.parse("2000-11-05")));
    }
}
